package com.yejhxt.controllerImpl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;

import com.yejhxt.inter.roleMapper;
import com.yejhxt.inter.userMapper;
import com.yejhxt.model.role;
import com.yejhxt.model.user;
import com.yejhxt.tools.ProjectTools;

public class UserController {
	private userMapper userMapper;
	private roleMapper roleMapper;

	public UserController(com.yejhxt.inter.userMapper userMapper, com.yejhxt.inter.roleMapper roleMapper) {
		super();
		this.userMapper = userMapper;
		this.roleMapper = roleMapper;
	}

	public int getMessage(String phoneNum, HttpSession session, String isforgot) {
		if (ProjectTools.isPhone(phoneNum)) {
			// System.out.println("tbl_sysuserMapper"+tbl_sysuserMapper);
			if (StringUtils.isEmpty(isforgot)) {
				user record = userMapper.selectByPhone(phoneNum);
				if (record == null) {
					try {
						String validateCode = ProjectTools.getMessage(phoneNum);
						session.setAttribute("validateCode", validateCode);
						session.setAttribute("phoneNumber", phoneNum);
					} catch (Exception e) {
						return 500;
					}
					return 200;
				} else
					return 403;
			} else { //忘记密码
				try {
					String validateCode = ProjectTools.getMessage(phoneNum);
					session.setAttribute("validateCode", validateCode);
					session.setAttribute("phoneNumber", phoneNum);
				} catch (Exception e) {
					return 500;
				}
				return 200;

			}
		}
		return 406;

	}

	public int register(String PhoneNumber, String Password, String validateCode, HttpSession session, String web) {
		String code = "";
		String phoneNumber = "";
		if (session != null) {
			code = (String) session.getAttribute("validateCode");
			phoneNumber = (String) session.getAttribute("phoneNumber");
		}
		if (StringUtils.equals(validateCode, code) && StringUtils.equals(phoneNumber, phoneNumber)) {
			if (userMapper.selectByPhone(PhoneNumber) == null) {//未注册
				user record;
				record = new user();
				record.setPhone(PhoneNumber);
				record.setPassword(Password);
				record.setCreatetime(new Date());
				record.setModifytime(new Date());
				userMapper.insertSelective(record);

				role role = new role();
				user done = userMapper.selectByPhone(record.getPhone());
				if (done != null) {
					
					role.setRoleid(done.getUserid());
				 role.setAddress("必填");
				 role.setEmail("必填");
				 role.setQq("必填");
				 role.setPhone("必填");
					role.setModifytime(new Date());
				  
					role.setRealname("未填写");
					role.setGender("男");
					
					roleMapper.insertSelective(role);
				} else
					return 500;

				session.setAttribute("User", done);
				session.setAttribute("Role", role);
				if (StringUtils.equals(web, "1")) {
					session.setMaxInactiveInterval(900);
				} else {
					session.setMaxInactiveInterval(-1);
				}
				return 200;
			} else {
				return 403;
			}
		} else {
			return 406;
		}
	}

	public int LoginHandler(String PhoneNumber, String Password, HttpSession session, String web) {

		if (userMapper.selectByPhone(PhoneNumber) == null) {
			return 406;
		} else {
			Map<String, String> map = new HashMap<String, String>();
			map.put("phone", PhoneNumber);
			map.put("password", Password);
			user record = userMapper.selectByPhoneAndPassword(map);
			// System.out.println(record + " \n " + map);
			if (record != null) {
				role role=roleMapper.selectByPrimaryKey(record.getUserid());
				if(role==null)
					return 403;
				session.setAttribute("User", record);
				session.setAttribute("Role", role);
				if (StringUtils.equals(web, "1")) {
					session.setMaxInactiveInterval(900);
				} else {
					session.setMaxInactiveInterval(-1);
				}
				return 200;
			} else
				return 403;
		}

	}

	public int PasswordUpdateHandler(String Password, String newPassword, HttpSession session, String web) {
		user u = (user) session.getAttribute("User");
		if (u != null) {
			String PhoneNumber = u.getPhone();
			if (userMapper.selectByPhone(PhoneNumber) == null) {
				return 406;
			} else {
				Map<String, String> map = new HashMap<String, String>();
				map.put("phone", PhoneNumber);
				map.put("password", Password);
				user record = userMapper.selectByPhoneAndPassword(map);
				// System.out.println(record + " \n " + map);
				if (record != null) {// password right
					record.setPassword(newPassword);
					userMapper.updateByPrimaryKey(record);
					return 200;
				} else
					return 403;
			}
		} else
			return 403;

	}

	public int PasswordForgotHandler(String PhoneNumber, String Password, String validateCode, HttpSession session,
			String web) {
		String code = "";
		String phoneNumber = "";
		if (session != null) {
			code = (String) session.getAttribute("validateCode");
			phoneNumber = (String) session.getAttribute("phoneNumber");
		}
		if (StringUtils.equals(validateCode, code) && StringUtils.equals(phoneNumber, phoneNumber)) {
			user record = userMapper.selectByPhone(PhoneNumber);
			if (record != null) {// 用户存在
				record.setPassword(Password);
				record.setModifytime(new Date());
				userMapper.updateByPrimaryKey(record);
				session.setAttribute("User", record);
				if (StringUtils.equals(web, "1")) {
					session.setMaxInactiveInterval(900);
				} else {
					session.setMaxInactiveInterval(-1);
				}
				return 200;
			} else {
				return 403;
			}
		} else {
			return 406;
		}
	}
}